#include <gtest/gtest.h>
#include "../mqserver/message.hpp"

using namespace std;
using namespace ns_rabbitmq;

MessageManager::MessageManagerPtr mptr;
class MessageTets : public testing::Environment
{
    virtual void SetUp() override
    {
        mptr = std::make_shared<MessageManager>("./data/message");
        mptr->InitQueueMsg("queue1");
    }
    virtual void TearDown()
    {
        //mptr->Clear();
        std::cout << "完成清理工作" << std::endl;
    }
};

TEST(message_test, insert_test)
{
    BasicProperties properties;
    properties.set_id(UUIDHelp::uuid());
    properties.set_deliver_mode(DeliveryMode::DURABLE);
    properties.set_routing_key("news.music.pop");
    // mptr->Insert("queue1", &properties, "hello world-1!", true);
    // mptr->Insert("queue1", nullptr, "hello world-2!", true);
    // mptr->Insert("queue1", nullptr, "hello world-3!", true);
    mptr->Insert("queue1", nullptr, "hello world-4!", false);
    ASSERT_EQ(mptr->GetDurable_count("queue1"), 3);
    ASSERT_EQ(mptr->GetTotal_count("queue1"), 3);
    ASSERT_EQ(mptr->GetSendable_count("queue1"), 4);
    ASSERT_EQ(mptr->GetWaitAck_count("queue1"), 0);
}

TEST(message_test, select_test)
{
    MessagePtr msgptr = mptr->Front("queue1");
    ASSERT_NE(msgptr, nullptr);
    ASSERT_EQ(mptr->GetWaitAck_count("queue1"), 1);
    ASSERT_EQ(mptr->GetSendable_count("queue1"), 3);
    ASSERT_EQ(msgptr->payload().body(), string("hello world-1!"));

    MessagePtr msgptr1 = mptr->Front("queue1");
    ASSERT_NE(msgptr1, nullptr);
    ASSERT_EQ(mptr->GetWaitAck_count("queue1"), 2);
    ASSERT_EQ(mptr->GetSendable_count("queue1"), 2);
    ASSERT_EQ(msgptr1->payload().body(), string("hello world-2!"));

    MessagePtr msgptr2 = mptr->Front("queue1");
    ASSERT_NE(msgptr2, nullptr);
    ASSERT_EQ(mptr->GetWaitAck_count("queue1"), 3);
    ASSERT_EQ(mptr->GetSendable_count("queue1"), 1);
    ASSERT_EQ(msgptr2->payload().body(), string("hello world-3!"));

    MessagePtr msgptr3 = mptr->Front("queue1");
    ASSERT_NE(msgptr3, nullptr);
    ASSERT_EQ(mptr->GetWaitAck_count("queue1"), 4);
    ASSERT_EQ(mptr->GetSendable_count("queue1"), 0);
    ASSERT_EQ(msgptr3->payload().body(), string("hello world-4!"));
}

// TEST(message_test, remove_test)
// {
//     MessagePtr msgptr = mptr->Front("queue1");
//     ASSERT_NE(msgptr, nullptr);
//     ASSERT_EQ(mptr->GetWaitAck_count("queue1"), 1);
//     ASSERT_EQ(mptr->GetSendable_count("queue1"), 3);
//     ASSERT_EQ(msgptr->payload().body(), string("hello world-1!"));
//     mptr->ack("queue1", msgptr->payload().basic_properties().id());
//     ASSERT_EQ(mptr->GetWaitAck_count("queue1"), 0);
//     ASSERT_EQ(mptr->GetSendable_count("queue1"), 3);
//     ASSERT_EQ(mptr->GetTotal_count("queue1"), 3);
//     ASSERT_EQ(mptr->GetDurable_count("queue1"), 2);
// }

int main(int argc, char *argv[])
{
    testing::InitGoogleTest(&argc, argv);
    testing::AddGlobalTestEnvironment(new MessageTets());
    RUN_ALL_TESTS();
    return 0;
}